const axios = require('axios');

const baseURL = 'http://localhost:7002/api/v1';

async function testAuth() {
  try {
    console.log('测试认证功能...\n');

    // 1. 测试登录
    console.log('1. 测试登录...');
    const loginResponse = await axios.post(`${baseURL}/user/login`, {
      username: 'demo',
      password: '123456'
    });
    
    console.log('登录结果:', loginResponse.data);
    
    if (loginResponse.data.code !== 200) {
      console.error('登录失败');
      return;
    }
    
    const token = loginResponse.data.data.token;
    console.log('获取到token:', token.substring(0, 20) + '...\n');

    // 2. 测试需要认证的接口
    const authHeaders = {
      'Authorization': `Bearer ${token}`
    };

    console.log('2. 测试用户信息接口...');
    const profileResponse = await axios.get(`${baseURL}/user/profile`, {
      headers: authHeaders
    });
    console.log('用户信息:', profileResponse.data);

    console.log('\n3. 测试推荐接口...');
    const recommendationResponse = await axios.get(`${baseURL}/recommendation/weather-based`, {
      headers: authHeaders,
      params: {
        weather: 'sunny',
        temperature: 25,
        location: '北京'
      }
    });
    console.log('推荐结果:', recommendationResponse.data);

    console.log('\n4. 测试分析接口...');
    const analysisResponse = await axios.get(`${baseURL}/analysis/clothing-usage`, {
      headers: authHeaders,
      params: {
        timeRange: '30d'
      }
    });
    console.log('分析结果:', analysisResponse.data);

    console.log('\n✅ 所有测试通过！');

  } catch (error) {
    console.error('❌ 测试失败:');
    if (error.response) {
      console.error('状态码:', error.response.status);
      console.error('响应数据:', error.response.data);
    } else {
      console.error('错误信息:', error.message);
    }
  }
}

testAuth();
